package com.jiaai.cloud.wx.controller;

import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.wx.entity.TWxgoods;
import com.jiaai.cloud.wx.service.ITWxgoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-22
 */
@Controller
@RequestMapping("/api/v1/twxgoods")
public class TWxgoodsController {
    @Autowired
    private ITWxgoodsService itWxgoodsService;

    //todo test no neccess. lazy to delete this method loooooooooooooool
    /**
     * 基本信息查询
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/findById/{goodsid}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object findById(@PathVariable("goodsid") Integer goodsid)  throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxgoodsService.selectById(goodsid));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 添加商品信息, 需要商店的id, 因为需要将商店和商品关联起来表示该商品是商店所发布的
     * @param tWxgoods
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/reg/{shopid}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    @Transactional
    public Object reg(@PathVariable("shopid") Integer shopid, @RequestBody TWxgoods tWxgoods)  throws ServiceException {
        try {
            itWxgoodsService.insert(tWxgoods);
            itWxgoodsService.insertShopGoods(shopid, tWxgoods.getGoodsid());
            return RestResultGenerator.genSuccessResult("添加商品成功");
        }catch (Exception e) {
            throw new ServiceException("添加商品失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 删除商品信息,并删除商店商品中间表
     * @param goodsid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/delete/{goodsid}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    @Transactional
    public Object delete(@PathVariable("goodsid") Integer goodsid)  throws ServiceException {
        try {
            itWxgoodsService.deleteShopGoods(goodsid);
            itWxgoodsService.deleteById(goodsid);
            return RestResultGenerator.genSuccessResult("删除商品成功");
        }catch (Exception e) {
            throw new ServiceException("删除失败", e.getMessage());
        }
    }


    //todo tested
    /**
     * 修改商品信息
     * @param goodsid
     * @param tWxgoods
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/update/{goodsid}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object update(@PathVariable("goodsid") Integer goodsid, @RequestBody TWxgoods tWxgoods)  throws ServiceException {
        try {
           tWxgoods.setGoodsid(goodsid);
            itWxgoodsService.updateById(tWxgoods);
            return RestResultGenerator.genSuccessResult("修改商品成功");
        }catch (Exception e) {
            throw new ServiceException("修改失败", e.getMessage());
        }
    }

    /**
     * 查询本人, 超级发布的, 商品列表, 分页
     * @param userid
     * @param frontPage
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/mysupergood",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryForMySuper(@RequestHeader Integer userid, FrontPage frontPage)  throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxgoodsService.queryForMySuper(userid, frontPage.getPagePlus()));
        }catch (Exception e) {
            throw new ServiceException("修改失败", e.getMessage());
        }
    }
}
